(function () {
    app.cp.register("ImportClientOrderController", ImportClientOrderController);
    function ImportClientOrderController($scope, $rootScope) {
        $rootScope.$on("importClientOrder", function (event) {
            $scope.importWindow.center().open();
            $(".k-window-content").removeClass('hidden');
            $scope.errorData.data([]);
            $scope.fileOrder.clearAllFiles();
            $scope.error=[];

        });
        $scope.fileOptions ={
            multiple: false,
            template: kendo.template($('#fileTemplate').html())
        };
        $scope.submit = function () {
            var button=  $("#submitBtn").data("kendoButton");
            var form = new FormData(document.getElementById("importOrder-form"));
            button.enable(false);
            $.ajax({
                type: "POST",
                url: contextPath+"/clientOrder/importOrder",
                enctype: 'multipart/form-data',
                data: form,
                cache: false,
                contentType: false,
                processData: false
            }).done(function (result) {
                if (result.success) {
                    kendo.factory.notifyInfo("导入成功");
                    $rootScope.reloadGrid();
                    $scope.errorData.data(result.data);
                    $scope.error = result.data;
                    $scope.$apply();
                } else {
                    kendo.factory.notifyError(result.message);
                }

            }).always(function () {
                button.enable(true);
            });
        };

        $scope.error = [];
        $scope.errorData = new kendo.data.DataSource({data:[]});
        $scope.close = function () {
            $scope.importWindow.close();
        };

        $scope.errorOptions = {
            columns: [{
                field: "rowIndex",
                title: "行号",
                width: 30,
                template: function (dataItem) {
                    return 2 + parseInt(dataItem.rowIndex);
                }
            }, {
                title: "结果",
                field: "success",
                template: "#=success?'成功':'失败'#",
                width: 50
            }, {
                title: "原因",
                field: "message",
                width: 300
            }]
        };
    }
})();